package edu.stanford.bmir.protege.web.client.ui.ontology.stkos.visualizer.ui;

import java.util.Date;

import com.google.gwt.core.client.GWT;
import com.google.gwt.http.client.URL;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Frame;
import com.google.gwt.user.client.ui.HTMLPanel;

import edu.stanford.bmir.protege.web.client.project.Project;
import edu.stanford.bmir.protege.web.client.ui.ontology.stkos.common.GWTExtUtil;

/**
 * Stkos Visualizer UI
 * 
 * @author LWS
 * 
 */
public class StkosVisualizerView extends Composite {
	interface StkosVisualizerViewUiBinder extends
			UiBinder<HTMLPanel, StkosVisualizerView> {

	}

	private static StkosVisualizerViewUiBinder ourUiBinder = GWT
			.create(StkosVisualizerViewUiBinder.class);

	private static final String baseURL = GWT.getHostPageBaseURL();
	
	private static final String PAGEBASEURL = baseURL+"stkosutil/visualizer/visualizer.html";

	private String iframeID;

	private Project project;

	@UiField
	protected Frame visualizerViewFrame;

	public StkosVisualizerView(Project project) {
		this.project = project;

		HTMLPanel rootElement = ourUiBinder.createAndBindUi(this);

		iframeID = "stkosutilVisualizerViewIframe" + "-"
				+ (new Date().getTime());

		if (visualizerViewFrame != null) {
			visualizerViewFrame.getElement().setId(iframeID);
			visualizerViewFrame.getElement().setAttribute("name", iframeID);
			visualizerViewFrame.getElement().setAttribute("width", "100%");
			visualizerViewFrame.getElement().setAttribute("height", "100%");
			visualizerViewFrame.getElement().setAttribute("scrolling", "no");
			visualizerViewFrame.getElement().setAttribute("frameborder", "0");
		}

		expandFrameUri();

		initWidget(rootElement);
		
		GWTExtUtil.hideProgressMonitor("提示", "程序正在处理、请稍候...", 850);
	}

	public void onRefresh() {
		expandFrameUri();
	}

	private void expandFrameUri() {
		if (visualizerViewFrame == null || project == null
				|| project.getProjectId() == null) {
			return;
		}
		String encodedProjectName = URL.encode(project.getProjectId().getId());
		String encodedIframeID = URL.encode(iframeID);
        String encodedBaseURL = URL.encode(baseURL);
        
		visualizerViewFrame.setUrl(PAGEBASEURL + "?projectId="
				+ encodedProjectName + "&iframeId=" + encodedIframeID+"&baseURL="+encodedBaseURL);
	}
}
